import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:travel/routes/index.dart';
import 'package:travel/ui/draw_under_line.dart';
import 'package:travel/utils/colors.dart';
import 'package:travel/utils/styles.dart';
import 'package:travel/utils/tools.dart';
import 'package:travel/screen/flutter_screenutil.dart';

class SettingController extends GetxController {
  RxBool isNotice = true.obs;

  RxBool isSound = true.obs;

  RxInt cacheSize = 50.obs;

  Rx<PackageInfo> packageInfo = Rx(PackageInfo(
    appName: 'Unknown',
    packageName: 'Unknown',
    version: 'Unknown',
    buildNumber: 'Unknown',
    buildSignature: 'Unknown',
    installerStore: 'Unknown',
  ));

  @override
  void onInit() {
    initPackageInfo();
    super.onInit();
  }

  Future<void> initPackageInfo() async {
    PackageInfo info = await PackageInfo.fromPlatform();
    packageInfo.value = info;
  }
}

class SettingView extends GetView<SettingController> {
  const SettingView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF7f7),
          appBar: AppBar(
            automaticallyImplyLeading: true,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back_ios,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
                size: 20.w,
              ),
            ),
            leadingWidth: 40.w,
            title: Text(
              '设置',
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.color3333, fontSize: 18.sp, fontWeight: FontWeight.bold),
            ),
          ),
          body: SingleChildScrollView(
            padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 20.w),
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  decoration:
                      BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
                  child: ListBody(
                    children: [
                      DrawUnderLine(
                          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                          child: InkWell(
                            onTap: () {
                              Get.toNamed(RoutePath.settingInfo);
                            },
                            child: SizedBox(
                              height: 50.h,
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    '个人信息',
                                    style: StylesUtils.customTextStyle(
                                        color: ColorsUtils.color3333, fontSize: 15.sp),
                                  ),
                                  Icon(
                                    Icons.arrow_forward_ios,
                                    color:
                                        ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                                    size: 15.sp,
                                  )
                                ],
                              ),
                            ),
                          )),
                      SizedBox(
                        height: 50.h,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '账号与安全',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color3333, fontSize: 15.sp),
                            ),
                            InkWell(
                                onTap: () {
                                  Get.toNamed(RoutePath.accountSafety);
                                },
                                child: Icon(
                                  Icons.arrow_forward_ios,
                                  color: ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                                  size: 15.sp,
                                ))
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 15.h),
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  decoration:
                      BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
                  child: ListBody(
                    children: [
                      DrawUnderLine(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                        child: SizedBox(
                          height: 50.h,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '消息设置',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color3333, fontSize: 15.sp),
                              ),
                              InkWell(onTap: () {
                                Get.toNamed(RoutePath.settingMessage);
                              }, child: Icon(
                                Icons.arrow_forward_ios,
                                color: ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                                size: 15.sp,
                              ),)
                            ],
                          ),
                        ),
                      ),
                      DrawUnderLine(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                        child: SizedBox(
                          height: 50.h,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '消息通知',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color3333, fontSize: 15.sp),
                              ),
                              IconButton(
                                  onPressed: () {
                                    controller.isNotice.value = !controller.isNotice.value;
                                  },
                                  icon: ToolsUtils.assetImageSvg(
                                      activeSvg(controller.isNotice.value),
                                      width: 45,
                                      height: 24))
                            ],
                          ),
                        ),
                      ),
                      DrawUnderLine(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                        child: SizedBox(
                          height: 50.h,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '点击音效',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color3333, fontSize: 15.sp),
                              ),
                              IconButton(
                                  onPressed: () {
                                    controller.isSound.value = !controller.isSound.value;
                                  },
                                  icon: ToolsUtils.assetImageSvg(
                                      activeSvg(controller.isSound.value),
                                      width: 45,
                                      height: 24))
                            ],
                          ),
                        ),
                      ),
                      DrawUnderLine(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                        child: SizedBox(
                          height: 50.h,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '本地缓存',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color3333, fontSize: 15.sp),
                              ),
                              Text.rich(TextSpan(children: [
                                TextSpan(
                                    text: '${controller.cacheSize.value}MB ',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: 15.sp, color: ColorsUtils.color6666)),
                                WidgetSpan(
                                    child: Icon(
                                      Icons.arrow_forward_ios,
                                      color: ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color9999),
                                      size: 15.sp,
                                    ),
                                    alignment: PlaceholderAlignment.middle)
                              ]))
                            ],
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 50.h,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '关于我们',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color3333, fontSize: 15.sp),
                            ),
                            TextButton(
                                onPressed: () {
                                  Get.toNamed(RoutePath.aboutUs);
                                },
                                child: Text.rich(TextSpan(children: [
                                  TextSpan(
                                      text: 'v${controller.packageInfo.value.version} ',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: 15.sp, color: ColorsUtils.color6666)),
                                  WidgetSpan(
                                      child: Icon(
                                        Icons.arrow_forward_ios,
                                        color: ColorsUtils.parseColorFromHexString(
                                            ColorsUtils.color9999),
                                        size: 15.sp,
                                      ),
                                      alignment: PlaceholderAlignment.middle)
                                ])))
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 15.h),
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  decoration:
                      BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
                  child: ListBody(
                    children: [
                      SizedBox(
                        height: 50.h,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '退出登录',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color3333, fontSize: 15.sp),
                            ),
                            Icon(
                              Icons.arrow_forward_ios,
                              color: ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                              size: 15.sp,
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ));
  }

  String activeSvg(bool value) {
    if (value) {
      return 'u2551.svg';
    }
    return 'u2552.svg';
  }
}
